package com.yewen.http.controller;

import com.alibaba.fastjson.JSONObject;
import com.yewen.http.core.vo.CommonResult;
import com.yewen.http.entity.EducationCounselor;
import com.yewen.http.service.EducationCounselorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SearchSchoolController
 * @Description 搜索对应的学校服务
 * @Date 2022/3/29 20:49
 * @Created by TaoVh
 */
@Controller
@RequestMapping("/yewen/search")
public class SearchController {
    
    @Autowired
    private EducationCounselorService educationCounselorService;

    @GetMapping("/bySchool")
    public ModelAndView bySchool(HttpServletRequest request,String universityName){
        ModelAndView view = new ModelAndView();
        view.setViewName("university_intro");
        /*查询服务，有空再写,这里先直接返回汕头大学*/
        view.addObject("school",universityName);
        view.addObject("id",request.getParameter("id"));
        return view;
    }
    @GetMapping("/byMajor")
    public ModelAndView byMajor(String major){
        ModelAndView view = new ModelAndView();
        view.setViewName("university_intro");

        return view;
    }

    /*根据传来的专业搜索导生*/
    @PostMapping("/byEduCounselorMajor")
    @ResponseBody
    public CommonResult byEduCounselor(String eduCounselorMajor){
        List<EducationCounselor> list;
        if(eduCounselorMajor.equals("ALL")||eduCounselorMajor.equals("")){
            list = educationCounselorService.getAllEduCounselors();
        }else {
            list = educationCounselorService.getEduCounselorsByMajor(eduCounselorMajor);
        }
        ArrayList arrayList = new ArrayList();
        for (EducationCounselor e: list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("guiderPro",e.getMajor());
            jsonObject.put("guiderName",e.getId());
            jsonObject.put("state",e.getState());
            arrayList.add(jsonObject);
        }
        return CommonResult.success(arrayList);
    }
}
